package com.goktech.olala.core.service.impl;

import com.goktech.olala.core.resp.RespBrandVo;
import com.goktech.olala.core.resp.RespCategoryVo;
import com.goktech.olala.core.service.IGoodCategoryService;
import com.goktech.olala.server.dao.goods.BrandMapper;
import com.goktech.olala.server.dao.goods.CategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;

/**
 * @author sanming
 * @Description 全部分类 + 品牌展示
 * @Date 2020/12/8 14:46
 */
@Service
public class GoodCategoryServiceImp implements IGoodCategoryService {

    @Autowired
    CategoryMapper categoryMapper;

    @Autowired
    BrandMapper brandMapper;

    /**
     * 一级、二级、三级分类
     * @return 包含一级、二级、三级分类的List
     */
    @Override
    public List<RespCategoryVo> beTreeList() {
        // 查询得到一级分类的List  respCategoryVos
        // 对象属性categoryName为一级分类
        List<RespCategoryVo> respCategoryVos = categoryMapper.selectByCategory();
        //品牌List
        List<RespBrandVo> brandVoList = brandMapper.selectBrand();

        // 用于最后存放二级和三级分类
        List<RespCategoryVo> secondList=null;
        // 三级分类
        List<RespCategoryVo> threeList=null;
        if(respCategoryVos!=null){
         //一级分类列表不为空，开始二级和三级分类目录的查询
            for (RespCategoryVo first:respCategoryVos){
                secondList=new ArrayList<>();
                //first为一级
                //得到分类id的数组字符串
                String categoryIdArr = first.getCategoryIdArr();
                //得到categoryId数组
                String[] categoryIdArray = categoryIdArr.split(",");
                //遍历categoryIdArray
                for (String cate:categoryIdArray){
                    // 得到Integer类型的categoryId
                    Integer categoryId = Integer.valueOf(cate);
                    //二级分类
                    List<RespCategoryVo> childList = categoryMapper.selectChildList(categoryId);
                    if (childList!=null){
                        //遍历secondList
                        for(RespCategoryVo second:childList){
                            Integer categoryId1 = second.getCategoryId();
                            //三级分类
                            threeList = categoryMapper.selectChildList(categoryId1);
                            second.setChildList(threeList);
                        }
                        secondList.addAll(childList);
                    }

                }
                //一级分类将二三级分类设置进自身属性
                first.setChildList(secondList);
                //分类出参添加查询出的品牌信息
                first.setBrandList(brandVoList);
            }
        }
        //返回包含一级、二级、三级分类和品牌信息的出参List
        return respCategoryVos;
    }

}
